// 示列 数据
import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLBoolean
} from 'graphql'

const queryObj = new GraphQLObjectType({
  name: 'myFirstQuery',
  description: 'a hello world demo',
  fields: {
    hello: {
      name: 'a hello world query',
      description: 'a hello world demo',
      type: GraphQLString,
      args: {
        name: {
          // 这里定义参数，包括参数类型和默认值
          type: GraphQLString,
          defaultValue: 'test'
        }
      },
      resolve(parentValue, args, request) {
        // 这里演示如何获取参数，以及处理
        return 'hello world ' + args.name + '!'
      }
    },
    person: {
      name: 'personQuery',
      description: 'query a person',
      type: new GraphQLObjectType({
        // 这里定义查询结果包含name,age,sex三个字段，并且都是不同的类型。
        name: 'person',
        fields: {
          name: {
            type: GraphQLString
          },
          age: {
            type: GraphQLInt
          },
          sex: {
            type: GraphQLBoolean
          }
        }
      }),
      args: {
        name: {
          type: GraphQLString,
          defaultValue: 'Charming'
        },
        age: {
          type: GraphQLInt,
          defaultValue: 2
        }
      },
      resolve(parentValue, args, request) {
        return {
          name: args.name || 'test',
          age: 20,
          sex: Math.random() > 0.5
        }
      }
    }
  }
})

export default new GraphQLSchema({
  query: queryObj
})
